<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class EssayClass extends Controller
{
    public function showclass(){
        $data=Db::name("essay_class")->select();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function getone(){
        $essay_class_id=input("id");
        $data=Db::name("essay_class")->where('essay_class_id',$essay_class_id)->find();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function insert(){
        $data=input();
        if(Db::name("essay_class")->insert($data)){
            $essay_class_id=Db::name("essay_class")->getLastInsID();
            return ['status'=>1,'message'=>'添加成功','essay_class_id'=>$essay_class_id];
        }
        else
            return ["status"=>0,'message'=>'添加失败'];
    }

    public function update(){
        $data=input();
        if(Db::name("essay_class")->where("essay_class_id",$data['id'])->setField($data['column'],$data['value']))
            return ['status'=>1,'message'=>'更新成功'];
        else
            return ['status'=>0,'message'=>'更新失败'];
    }

    public function delete(){
        $essay_class_id=input("id");
        if(Db::name("essay_class")->where("essay_class_id",$essay_class_id)->delete())
            return ['status'=>1,'message'=>'删除成功'];
        else
            return ['status'=>0,'message'=>'删除失败'];
    }

    public function showTreeClass(){
        $datatree=Db::name("essay_class")->where("essay_parent_id",0)->select();
        $datachild=Db::name("essay_class")->where("essay_parent_id",">",0)->select();
        $dataone=[];
        $float=0;
        for($i=0;$i<count($datachild);$i++){
            $float=0;
            for($j=0;$j<count($datatree);$j++){
                if($datachild[$i]['essay_parent_id']==$datatree[$j]['essay_class_id']){
                    if(empty($datatree[$j]['child']))
                        $datatree[$j]['child']=[];
                    array_push($datatree[$j]['child'],$datachild[$i]);                    
                    $float=1;
                }
            }
            if($float==0)
                array_push($dataone,$datachild[$i]);
        }
        for($z=0;$z<count($dataone);$z++)
            array_push($datatree,$dataone[$z]);
        return ['status'=>1,'message'=>'获取成功','data'=>$datatree];
    }
}